Skip to content

fix: stop corrupting SQLAlchemy's shared IdentifierPreparer.reserved_words - #58

Open
rusackas wants to merge 1 commit into
Kyligence:masterfrom
rusackas:fix/reserved-words-shared-state
Open

rusackas wants to merge 1 commit into
Kyligence:masterfrom
rusackas:fix/reserved-words-shared-state

Conversation

@rusackas

@rusackas rusackas commented Oct 2, 2026

Copy link
Copy Markdown

Summary

KylinIdentifierPreparer's class body mutates sqlalchemy.sql.compiler.IdentifierPreparer.reserved_words directly:

class KylinIdentifierPreparer(compiler.IdentifierPreparer):
    compiler.IdentifierPreparer.reserved_words = \
        set(itertools.chain(*[[e.lower(), e] for e in CALCITE_KEYWORDS]))
    compiler.IdentifierPreparer.reserved_words.update(SUPERSET_KEYWORDS)

That's the base class's attribute, not KylinIdentifierPreparer's own. Since this executes at class-definition/import time, just importing kylinpy.sqla_dialect (e.g. via SQLAlchemy's dialect entry-point registry enumerating installed dialects, which doesn't require actually connecting to Kylin) replaces the shared default that every other SQLAlchemy dialect without its own reserved_words falls back on, for the rest of the process. Ordinary identifiers like name become globally "reserved" and start getting unexpectedly quoted in unrelated dialects' generated SQL.

Reproduced directly:

from sqlalchemy.sql import compiler
before = set(compiler.IdentifierPreparer.reserved_words)
import kylinpy.sqla_dialect
after = compiler.IdentifierPreparer.reserved_words
# before: 94 entries, 'name' not reserved
# after: 1281 entries, 'name' IS reserved -- for every dialect, not just Kylin's

We (Apache Superset) hit this in production: loading kylinpy's dialect alongside other database dialects in the same process caused unrelated databases' queries to start quoting ordinary column names. We worked around it on our side (snapshotting/restoring the shared attribute around dialect loading), but the real fix belongs here.

Fix

Assign to KylinIdentifierPreparer's own reserved_words instead of the base class's, matching the pattern SQLAlchemy's built-in dialects (postgresql, mysql, etc.) use for exactly this reason. Kylin's own effective reserved-word set is unchanged -- same CALCITE_KEYWORDS/SUPERSET_KEYWORDS union, same values -- only the cross-dialect leakage into the shared base class is removed.

Testing

Added tests/test_reserved_words.py:

  • One test runs in a subprocess (since the corruption, once it happens, persists for the rest of any process -- a plain unit test can't reliably observe "before" once something else in the same test session has already imported the module) confirming compiler.IdentifierPreparer.reserved_words is unchanged after importing kylinpy.sqla_dialect.
  • One test confirms KylinIdentifierPreparer.reserved_words still has Kylin's own full keyword set.

Both fail red against the current code on master and pass green with this fix. Also ran the rest of the suite that doesn't require a live Kylin sandbox (test_compat, test_cube_source, test_kylin_types, test_kylinpy, test_sqla_types, test_table_source -- 26 tests) and confirmed no regressions.

Note on repo activity

I noticed this repo hasn't had a commit since early 2023. I'm filing this anyway since it's a real, narrow, low-risk correctness bug with a minimal fix and clear reproduction -- happy to help in any way if there's interest in picking it back up, and no worries if not.

🤖 Generated with Claude Code

…words

KylinIdentifierPreparer's class body assigned directly to
sqlalchemy.sql.compiler.IdentifierPreparer.reserved_words (the base
class) instead of its own subclass attribute. Since this runs at
class-definition/import time, merely importing kylinpy.sqla_dialect
(e.g. via SQLAlchemy's dialect registry enumerating installed
entry points) replaced the shared default every other SQLAlchemy
dialect without its own reserved_words falls back on, for the rest
of the process. Ordinary identifiers like "name" became globally
"reserved" and started getting quoted in unrelated dialects' output.

Fix: assign to KylinIdentifierPreparer's own reserved_words instead,
the same pattern SQLAlchemy's built-in dialects use. Kylin's own
reserved-word set (CALCITE_KEYWORDS + SUPERSET_KEYWORDS) is unchanged;
only the leakage into the shared base class is removed. Verified with
a regression test (subprocess-isolated, since the corruption persists
for the rest of any process that already imported the old code) that
fails red against the previous version and passes green here, plus a
full run of the rest of the test suite that doesn't require a live
Kylin server.

Co-Authored-By: Evan Rusackas <evan@preset.io>
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant